package presenter;

import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.File;

public class TextOutput extends JLabel implements ActionListener, ImageObserver {
    String out;
    String[] output;
    Font font;
    int fontSize;
    int width;
    int height;
    //Sprite background;
    private boolean video;
    int id=0;
    int verse=0;
    Timer t;
    JFrame outputFrame;
    private boolean clear;
    private boolean live;
    private boolean capitalized;
    private JLayeredPane jlp;

    private SongFile sf;
    private Image background;
    private JFrame j;
    private SongFile defaultSong, current;
    private SongVerse currentVerse;
    private SimpleVariableFile textInformation;

    public TextOutput() {
            this(800,600);
    }

    public TextOutput(int width,int height) {
        t= new Timer(40,this);
        t.start();
        out="<html>";
        this.width=width;
        this.height=height;
        setSize(width,height);
        setHorizontalAlignment(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        fontSize=12;
        textInformation=new SimpleVariableFile("TextInformation.txt");
        if(textInformation.exists("WrapLimit")) {
            TextInformation.wrapLimit=Integer.parseInt(textInformation.getVariable("WrapLimit"));
            System.out.println(TextInformation.wrapLimit);
        }
        if(textInformation.exists("Offset(1/x)")) {
            TextInformation.divisor=Double.parseDouble(textInformation.getVariable("Offset(1/x)"));
        }
        if(textInformation.exists("FontSize")) {
            TextInformation.staticFontSize=true;
            TextInformation.fontSize=Integer.parseInt(textInformation.getVariable("FontSize"));
        }
        

        clear=false;
        capitalized=true;
        jlp=new JLayeredPane();

        video=false;


        jlp.add(this);
        //jlp.moveToFront(jb);

        setForeground(Color.WHITE);
        jlp.setSize(width,height);
        jlp.setVisible(true);

        outputFrame=new JFrame("EMP Output");
        outputFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("emp.png"));
        outputFrame.setContentPane(this);
        outputFrame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);	
        outputFrame.setUndecorated(true);
        outputFrame.setSize(width,height);
        //jd.setVisible(true);
        if(textInformation.exists("Font")) {
                    setFont(new Font(textInformation.getVariable("Font"),0,TextInformation.fontSize));
        }
        
        

    }
    
    public void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;
      g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      super.paintComponent(g);
      //g.drawImage(background, 0, 0,getWidth(),getHeight(), this);
    }

    public void setDefaultSong(SongFile defaultSong) {
        this.defaultSong=defaultSong;
        if(output==null||!current.hasCustomBackground()) {
            setBackground(new File(defaultSong.getBackground()));
        }
        if(current==null||!current.hasCustomColor()) {
            setForeground(defaultSong.getColor());
        }
        if(background.getWidth(labelFor)!=getWidth()) {
                Image temp=background;
                temp=temp.getScaledInstance(getWidth(), getHeight(), 0);
                background=temp;
                setIcon(new ImageIcon(background));
        }
        
    }

    public void setSong(SongFile song) {
        current=song;
        if(current.hasCustomBackground()) {
            setBackground(new File(current.getBackground()));            
        } else {
            setBackground(new File(defaultSong.getBackground()));
        }

        if(current.hasCustomColor()) {
            setForeground(current.getColor());
        } else {
            setForeground(defaultSong.getColor());
        }
        if(!isVisible()) {
            setVisible(true);
        }
        clear=false;
    }


    public void setCapitalized(boolean b) {
        capitalized=b;
    }

    public void setUndecorated(boolean b) {
        if(outputFrame.isVisible()) {
            outputFrame.setVisible(false);
            outputFrame.setUndecorated(b);
            outputFrame.setVisible(true);
        } else {
            outputFrame.setUndecorated(b);
        }    
    }

    /*public void setVisible(boolean b) {
        outputFrame.setVisible(b);
    }*/


    public void clear() {
        if(clear) {
            if(output!=null){
                //System.out.println(output[0]);
            setText(output);}
        } else {
            setText("");
        }
        clear=!clear;
    }

    public boolean getLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live=live;
    }
    public void setClear(boolean clear) {
        this.clear=clear;
    }

    public void black() {
        if(isVisible()) {
            outputFrame.setBackground(Color.BLACK);
            setVisible(false);
        } else {
            outputFrame.setBackground(Color.GRAY);
            setVisible(true);
        }
    }

    public int getID() {
        if(current==null)
            return 0;
        return current.getNumber();
    }

    public int getVerse() {
        return verse;
    }

    public boolean hasVideo() {
        return video;
    }

    public void setVideo(boolean b) {
        video=b;
    }
    public void setVerse(SongVerse verse) {
        this.currentVerse=verse;
    }

    public void setVerse(int verse) {
        setVerse(current.getVerse(verse));
        this.verse=verse;
        setText(currentVerse);
    }
    public void setFont(String font) {
            this.font=new Font(font,0,TextInformation.fontSize);
            setFont(this.font);
            System.out.println(this.font.getSize());
    }

    public void setText(SongVerse verse) {
        String[] output= verse.getFormatted(TextInformation.wrapLimit);
        for(int i=0; i<output.length; i++) {
            //System.out.println(output[i]);
        }
        setText(output);
    }

    public void setText(String[] output) {
            if(output==null) {
                output=new String[1];
                output[0]=" ";
            }
            this.output=output;
            String temp="";
            out="<html>  <p style=\"text-align: center;\"><b>";
            for(int j=0;j<output.length; j++) {
                temp=output[j];
                /*if(output[j].length()>Global.wrapLimit) {

                    int start=output[j].length()/2;
                    for(int i=0; i<output[j].length()/2; i++) {
                        String x=output[j].substring(start-i,start-i+1);
                        String y=output[j].substring(start+i,start+i+1);
                        if(x.equals(" ")) {
                            temp=output[j].substring(0,start-i+1)+"<br>"+output[j].substring(start-i,output[j].length());
                            //System.out.println("updated to "+temp);
                            break;
                        } else if(y.equals(" ")) {
                            temp=output[j].substring(0,start+i+1)+"<br>"+output[j].substring(start+i,output[j].length());
                            //System.out.println("updated to "+temp);
                            break;    
                        }
                    }

                }*/
                if(capitalized)
                    temp=temp.toUpperCase();
                //System.out.println(output[i]);

                out+=temp+"<br>";
            }
            out+="</b></p></html>";

            super.setText(out);
            if(!TextInformation.staticFontSize)
                updateFont();
            //etFont(font);

            
    }

    public boolean isClear() {
        return clear;
    }

    public void updateFont() {
            Graphics g=getGraphics();
            
           
            if(font==null) {
                font=g.getFont();
                if(textInformation.exists("Font")) {
                    setFont(textInformation.getVariable("Font"));
                }
            }
                    
            if(g!=null) {


            FontMetrics fm   = g.getFontMetrics(font);
            int height=0;
            int maxLength=0;
            int largestWidth=0;

            for(int index=0; index<output.length; index++){
                    java.awt.geom.Rectangle2D rect = fm.getStringBounds(output[index].toUpperCase(), g);
                    height+=(int)rect.getHeight();
                    //System.out.println(output[index]);
                    if((int)rect.getWidth()>largestWidth){
                            maxLength=index;
                            largestWidth=(int)rect.getWidth();
                    }
            }

            double heightRatio=(this.getHeight()-(this.getHeight()/TextInformation.divisor))/(double)height;
            double widthRatio=(this.getWidth()-(this.getWidth()/TextInformation.divisor))/(double)largestWidth;
            //System.out.println(largestWidth+" "+widthRatio+" "+height+" "+heightRatio);
            if(heightRatio>=widthRatio){
                    fontSize=(int)(fontSize*widthRatio);
                    height=(int)(height*widthRatio);
            }
            else {
                    fontSize=(int)(fontSize*heightRatio);
                    height=(int)(height*heightRatio);

            }

            setFont(font.getFontName());
            g.setFont(font);
            g.setColor(Color.RED);
            setFont(font);
            //super.getGraphics().setColor(Color.RED);
            //System.out.println("font size "+font.getSize());
            }
    }

    public void setBackground (File f) {
        background=new ImageIcon(Toolkit.getDefaultToolkit().getImage(f.getPath())).getImage();
        setBackground(background);
       // setIcon(null);
    }


    public void changeLocation(int x, int y) {
        outputFrame.setLocation(x,y);
        //System.out.println(outputFrame.getLocation());
    }

    public void changeSize(int x, int y) {
        outputFrame.setSize(x,y);
        setSize(x,y);
        setBackground(background);
    }
    public void setBackground(Image img) {
        if(img!=null) {
            //System.out.println(img.getWidth(this)+" "+img.getHeight(this));
            setIcon(new ImageIcon(img));
            background=img;
            if(background.getWidth(this)!=getWidth()) {
                    background=background.getScaledInstance(getWidth(), getHeight(), 0);
                    
                    setIcon(new ImageIcon(background));
            }
        } else {
            System.out.println("image is null");
        }
    }

    public JFrame getFrame() {
        return outputFrame;
    }



        
    @Override
    public void setBounds (Rectangle bounds) {
            outputFrame.setBounds(bounds);
            changeSize((int)bounds.getWidth(),(int)bounds.getHeight());
    }

    public void actionPerformed(ActionEvent event) {
        repaint();
    }
}